package com.fet.redeem.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.fet.redeem.dao.RedeemDao;
import com.fet.redeem.model.Customer;
import com.fet.redeem.model.Gift;

public class RedeemDaoImpl extends JdbcDaoSupport implements RedeemDao {

	private RedeemDao redeemDao;

	public RedeemDao getRedeemDao() {
		return redeemDao;
	}

	public void setRedeemDao(RedeemDao redeemDao) {
		this.redeemDao = redeemDao;
	}
	
	public List<Customer> getAllCustomers(){
		List<Customer> custList = new ArrayList<Customer>();
		String sql = "select * from customer";
		List<Map<String,Object>> list = this.getJdbcTemplate().queryForList(sql);
		if(list != null){
			for(Map<String,Object> map:list){
				Customer custmer = new Customer();
				custmer.setName((String)map.get("name"));
				custmer.setMsisdn((String)map.get("msisdn"));
				custmer.setCustomerNumberA((String)map.get("customerNumberA"));
				custmer.setStartDate((Date)map.get("startDate"));
				custmer.setEndDate((Date)map.get("endDate"));
				custmer.setStatus((String)map.get("status"));
				custmer.setVipGrade((String)map.get("vipGrade"));
				custList.add(custmer);
			}
		}
		return custList;
	}
	
	public List<Gift> getAllGifts(){
		List<Gift> giftList = new ArrayList<Gift>();
		String sql = "select * from gift";
		giftList = this.getJdbcTemplate().query(sql, new ResultSetExtractor<List<Gift>>(){
			public List<Gift> extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				List<Gift> giftList = new ArrayList<Gift>();
				while(rs.next()){
					Gift gift = new Gift();
					gift.setId(rs.getString(1));
					gift.setName(rs.getString(2));
					gift.setPrice(rs.getString(3));
					giftList.add(gift);
				}
				return giftList;
			}
		});
		return giftList;
	}
	
}
